package com.semis.web.controller.base;

import com.alibaba.fastjson2.JSONObject;
import com.semis.business.base.domain.BasLandSystem;
import com.semis.business.base.service.IBasLandSystemService;
import com.semis.common.annotation.Log;
import com.semis.common.core.controller.BaseController;
import com.semis.common.core.domain.AjaxResult;
import com.semis.common.core.domain.ListSelect;
import com.semis.common.core.domain.model.LoginUser;
import com.semis.common.core.page.TableDataInfo;
import com.semis.common.enums.BusinessType;
import com.semis.common.utils.poi.ExcelUtil;
import com.semis.business.system.service.ISysFilesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 地块系统数据Controller
 *
 * @author litu
 * @date 2023-07-17
 */
@Api(tags = "地块系统数据管理")
@RestController
@RequestMapping("/basedata/landsystem")
public class BasLandSystemController extends BaseController {
    @Autowired
    private IBasLandSystemService basLandSystemService;

    @Autowired
    private ISysFilesService sysFilesService;

    /**
     * 查询地块系统数据列表
     */
    @ApiOperation("查询地块系统数据列表")
    @PreAuthorize("@ss.hasPermi('basedata:landsystem:list')")
    @GetMapping("/list")
    public TableDataInfo list(BasLandSystem basLandSystem) {
        startPage();
        LoginUser user = getLoginUser();
        List<BasLandSystem> list = basLandSystemService.selectBasLandSystemList(basLandSystem, user.getUser());
        return getDataTable(list);
    }

    /**
     * 查询地块系统数据列表（不分页）
     * 土壤转运概况表单下拉使用
     */
    @ApiOperation("查询地块系统下拉列表")
    @GetMapping("/select")
    public AjaxResult select(BasLandSystem basLandSystem) {
        LoginUser user = getLoginUser();
        List<ListSelect> list = basLandSystemService.selectBasLandSystemSelect(basLandSystem, user.getUser());
        return success(list);
    }

    // /**
    //  * 导出地块系统数据列表
    //  */
    // @ApiOperation("导出地块系统数据列表")
    // @PreAuthorize("@ss.hasPermi('basedata:landsystem:export')")
    // @Log(title = "地块系统数据", businessType = BusinessType.EXPORT)
    // @PostMapping("/export")
    // public void export(HttpServletResponse response, BasLandSystem basLandSystem) {
    //     String userType = getLoginUser().getUser().getUserType();
    //     Long userId = getLoginUser().getUserId();
    //     List<BasLandSystem> list = basLandSystemService.selectBasLandSystemList(basLandSystem, userType, userId);
    //     ExcelUtil<BasLandSystem> util = new ExcelUtil<BasLandSystem>(BasLandSystem.class);
    //     util.exportExcel(response, list, "地块系统数据数据");
    // }

    /**
     * 导入地块系统数据列表
     */
    @ApiOperation("导入地块系统数据列表")
    @Log(title = "地块系统数据", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('basedata:landsystem:import')")
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<BasLandSystem> util = new ExcelUtil<BasLandSystem>(BasLandSystem.class);
        List<BasLandSystem> list = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = basLandSystemService.importLand(list, updateSupport, operName);
        return success(message);
    }

    /**
     * 导入地块系统模板
     */
    @ApiOperation("导入地块系统模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<BasLandSystem> util = new ExcelUtil<BasLandSystem>(BasLandSystem.class);
        util.encodingFilename("导入地块系统模板.xlsx");
        util.importTemplateExcel(response, "地块数据");
    }

    /**
     * 获取地块系统数据详细信息
     */
    @ApiOperation("获取地块系统数据详细信息,依据地块编码信息")
    // TODO 后续重新梳理下权限
//    @PreAuthorize("@ss.hasPermi('basedata:landsystem:query')")
    @GetMapping(value = "/{code}")
    public AjaxResult getInfo(@PathVariable("code") String code) {
        return success(basLandSystemService.getByCode(code));
    }

    /**
     * 修改地块系统数据
     */
    @ApiOperation("修改地块系统数据")
    @PreAuthorize("@ss.hasPermi('basedata:landsystem:edit')")
    @Log(title = "地块系统数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasLandSystem basLandSystem) {
        return toAjax(basLandSystemService.updateById(basLandSystem));
    }

    /**
     * 获取坐标点周围的地块系统数据
     */
    @ApiOperation("获取坐标点周围的地块系统数据")
    @GetMapping("/getSurroundingLand")
    public AjaxResult getSurroundingLand(@RequestParam Float longitude,@RequestParam Float latitude,@RequestParam Integer extent) {
        List<JSONObject> landMsgs = basLandSystemService.getSurroundingLand(longitude, latitude, extent);
        return success(landMsgs);
    }
}
